import 'reflect-metadata';
import { Connection, getConnection, createConnection,DataSource  } from 'typeorm';
import { Users } from './entity/index';

let connectionReadyPromise: Promise<Connection> | null = null;

export const prepareConnection = () => {

    const AppDataSource = new DataSource({
        type: "mysql",
        host: "127.0.0.1",
        port: 3306,
        username: "root",
        password: "aA123456",
        database: "new_schema_test",
    })

    AppDataSource.initialize()
        .then(() => {
            console.log("数据源已初始化！")
        })
        .catch((err) => {
            console.error("数据源初始化时出错", err)
        })
  // if (!connectionReadyPromise) {
  //   connectionReadyPromise = (async () => {
  //     try {
  //         console.log(`%c111`, "color:red");
  //       const staleConnection = await getConnection();
  //         console.log(`%c11222221`, "color:red");
  //       await staleConnection.close();
  //     } catch (error) {
  //       console.log(error);
  //         console.log(`%c errorerrorerrorerrorerror}`, "color:red");
  //     }
  //       console.log(`%c 3333}`, "color:red");
  //       console.log(`%c 111}`, "color:red");
  //     const connection = await createConnection({
  //       type: 'mysql',
  //       host:'127.0.0.1',
  //         port:3306,
  //         username:'root',
  //         password:"aA123456",
  //       database:"new_schema_test",
  //       entities: [Users],
  //       synchronize: false,
  //       logging: true,
  //     });
  //       console.log(`%c 2222}`, "color:red");
  //     return connection;
  //   })();
  // }
  //
  // return connectionReadyPromise;
};
